package com.customization.unionpay.market.dao;

import com.alibaba.fastjson.JSONObject;
import com.alitriph5.logging.Logger;
import com.alitriph5.logging.LoggerFactory;
import com.customization.unionpay.market.util.FormUtil;
import weaver.conn.RecordSet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ApplicationAdjustmentDao {

    public Logger log = LoggerFactory.getLogger(ApplicationAdjustmentDao.class);


    public List<JSONObject> queryByBdhs(List<String> bdh) {
        //查询传过来的要校验的营销活动事项
        StringBuilder querySql = new StringBuilder();
        querySql.append("SELECT ")
                .append("a.requestid, ")
                .append("a.PROJECT_CODE, ")
                .append("b.yskm, ")
                .append("b.yszrzx, ")
                .append("b.nbczje, ")
                .append("b.bcdzje, ")
                .append("a.bdh, ")
                .append("a.yskzq, ")
                .append("a.ywlx, ")
                .append("a.sqr, ")
                .append("a.yxptbz, ")
                .append("a.bzgs, ")
                .append("a.bzbm, ")
                .append("b.jjsx ")
                .append("FROM ")
                .append(com.customization.unionpay.market.util.FormUtil.APPLICATION_ADJUSTMENT)
                .append(" a ")
                .append("LEFT JOIN ")
                .append(FormUtil.APPLICATION_ADJUSTMENT_DETAILS)
                .append(" b ON a.id = b.mainId ")
                .append("WHERE a.bdh IN (");
        bdh.forEach(o -> {
            querySql.append(o).append(",");
        });
        querySql.deleteCharAt(querySql.length() - 1).append(")");
        try {
            RecordSet rs = new RecordSet();
            rs.execute(querySql.toString());
            List<JSONObject> applicationList = new ArrayList<>();
            while (rs.next()) {
                JSONObject o = new JSONObject();
                o.put("requestid", rs.getString("requestid"));
                o.put("PROJECT_CODE", rs.getString("PROJECT_CODE"));
                o.put("yskm", rs.getString("yskm"));
                o.put("yszrzx", rs.getString("yszrzx"));
                o.put("ysqjn", rs.getString("yskzq").substring(0, 4));
                o.put("nbczje", rs.getString("nbczje"));
                o.put("bctzje", rs.getString("bcdzje"));
                o.put("bdh", rs.getString("bdh"));
                o.put("yskzq", rs.getString("yskzq"));
                o.put("ywlx", rs.getString("ywlx"));
                o.put("sqr", rs.getString("sqr"));
                o.put("lyxt", "智慧财务系统");
                o.put("sm", rs.getString("yxptbz"));
                o.put("bzgs", rs.getString("bzgs"));
                o.put("bzbm", rs.getString("bzbm"));
                o.put("jjsx", rs.getString("jjsx"));
                applicationList.add(o);
            }
            return applicationList;
        } catch (Exception e) {
            log.error("根据表单号查询营销事项申请调整单失败");
        }
        return null;
    }

    public Map<String,Object> queryBySqm(String sqm) {
        //查询传过来的要校验的营销活动事项
        StringBuilder querySql = new StringBuilder();
        querySql.append("WITH RankedData AS (")
                .append("SELECT ")
                .append("a.requestid, a.PROJECT_CODE, b.yskm, b.yszrzx, b.nbczje, b.bcdzje, a.bdh, a.yskzq, a.ywlx, a.sqr, a.yxptbz, a.bzgs, a.bzbm, b.jjsx, ")
                .append(" ROW_NUMBER() OVER (PARTITION BY a.yxpthdsqm ORDER BY a.ywrq DESC) AS rn ")
                .append("FROM ")
                .append(" APPLICATION_ADJUSTMENT a ")
                .append("LEFT JOIN ")
                .append("APPLICATION_ADJUSTMENT_DETAILS b ON a.id = b.mainId ")
                .append("WHERE ")
                .append("a.yxpthdsqm = ").append(sqm).append(") ")
                .append("SELECT ")
                .append(" requestid, PROJECT_CODE, yskm, yszrzx, nbczje, bcdzje, bdh, yskzq, ywlx, sqr, yxptbz, bzgs, bzbm, jjsx ")
                .append("FROM ")
                .append(" RankedData ")
                .append("WHERE ")
                .append(" rn = 1;");
        Map<String, Object> result =new HashMap<>();
        try {
            RecordSet rs = new RecordSet();
            rs.execute(querySql.toString());
            while (rs.next()) {
                result.put("requestid", rs.getString("requestid"));
                result.put("PROJECT_CODE", rs.getString("PROJECT_CODE"));
                result.put("yskm", rs.getString("yskm"));
                result.put("yszrzx", rs.getString("yszrzx"));
                result.put("ysqjn", rs.getString("yskzq").substring(0, 4));
                result.put("nbczje", rs.getString("nbczje"));
                result.put("bctzje", rs.getString("bcdzje"));
                result.put("bdh", rs.getString("bdh"));
                result.put("yskzq", rs.getString("yskzq"));
                result.put("ywlx", rs.getString("ywlx"));
                result.put("sqr", rs.getString("sqr"));
                result.put("lyxt", "智慧财务系统");
                result.put("sm", rs.getString("yxptbz"));
                result.put("bzgs", rs.getString("bzgs"));
                result.put("bzbm", rs.getString("bzbm"));
                result.put("jjsx", rs.getString("jjsx"));
            }
        } catch (Exception e) {
            log.error("根据表单号查询营销事项申请调整单失败", e);
        }

        return result;
    }
}
